package com.aijk.web.admin.plugs.aop;

import com.aijk.commons.mq.utils.MqUtils;
import com.aijk.commons.utils.IpUtils;
import com.aijk.config.MqConfig;

import com.aijk.ehm.sys.api.LogApi;
import com.aijk.ehm.sys.entity.annotation.SysLogger;
import com.aijk.ehm.sys.model.SysLog;
import com.aijk.web.admin.shiro.ShiroDbRealm;
import com.alibaba.rocketmq.client.producer.SendResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * Created by lenovo on 2016/1/12.
 */
public class LoggerHandler {


    @Autowired
    private LogApi logApi;

    private  ShiroDbRealm.ShiroUser  shiroUser;

    @SuppressWarnings("unused")
    private Object  dealMessage(ProceedingJoinPoint point) throws Throwable {
        // System.out.println(args.length );
        // Class targetClass = point.getTarget().getClass();
        // Method method = targetClass.getMethod(methodName);

        final Object[] args = point.getArgs();
  /*      final RetMsg msg = (RetMsg) retVal;*/
       // System.out.println(args.length );
      //  ShiroUser user = getShiroUser();
        Class targetClass = point.getTarget().getClass();
    /*    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();*/
        SysLogger logger =getLoggerAnnotation(point);
        SysLog log =new SysLog();
        log.setLogIp(IpUtils.getRealIp());
        log.setSysIdentify("web");
        log.setLogDate(new Date());
        log.setSysCode("eh");
        log.setLogAcount(getShiroUser().getLoginName());
        log.setLogUser(getShiroUser().getName());
        log.setLogDesc(logger.desc());
        log.setLogModule(logger.module());
        log.setLogOperate(logger.operate().name());
      //  logService.insertLog(log);
        SendResult re = MqUtils.sendMessage(MqConfig.GROUP_SYS_LOG, MqConfig.TOPIC_SYS_LOG,MqConfig.TAG_SYS_LOG, String.valueOf(System.currentTimeMillis()), log);
        Object retVal = point.proceed();
        return retVal;
    }

    // 获取方法的中文备注____用于记录用户的操作日志描述
    public static SysLogger getLoggerAnnotation(ProceedingJoinPoint joinPoint)
            throws Exception {
        SysLogger logger =null;
        String targetName = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] arguments = joinPoint.getArgs();

        Class targetClass = Class.forName(targetName);
        Method[] method = targetClass.getMethods();
        String methode = "";
        for (Method m : method) {
            if (m.getName().equals(methodName)) {
                Class[] tmpCs = m.getParameterTypes();
                if (tmpCs.length == arguments.length) {
                     logger = m.getAnnotation(SysLogger.class);
                  /*  if (logger != null) {
                        methode = logger.module();
                    }*/
                    break;
                }
            }
        }
        return logger;
    }
   /* protected ShiroUser getShiroUser() {
        Subject currentUser = SecurityUtils.getSubject();

        return currentUser.getPrincipals() != null ? (ShiroUser) currentUser.getPrincipals().getPrimaryPrincipal() : null;
    }*/

    protected ShiroDbRealm.ShiroUser getShiroUser(){
        if(shiroUser==null){
            Subject currentUser = SecurityUtils.getSubject();
            return currentUser.getPrincipals() != null ? (ShiroDbRealm.ShiroUser) currentUser.getPrincipals().getPrimaryPrincipal() : null;
        }
        return shiroUser;
    }



}
